package com.itzhigui.controller.api;

import com.itzhigui.dto.UserDto;
import com.itzhigui.entity.User;
import com.itzhigui.service.UserService;
import com.itzhigui.utils.ConstantUtil;
import com.itzhigui.utils.ResponseEnum;
import com.itzhigui.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 *
 */
@RestController
@Slf4j
@RequestMapping(value = "/api/user")
@Api(value = "前台用户接口" ,tags = "ApiUserController") // 对控制器的描述
public class ApiUserController {
    @Resource
    private UserService userService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation(value = "前台登录" ,notes = "前台登录，需要提供用户名和密码")
    @ApiImplicitParam(value = "前台登录" ,name = "user" ,required = true, dataType = "User")
    @PostMapping(value = "/login")
    public ResponseUtil login(
            @RequestBody User user,
            HttpSession session
    ) {
        ResponseUtil r = userService.apiLogin(user);
        if (r.getCode().intValue() == 1008){
            //登录成功并绑定session
            session.setAttribute(ConstantUtil.LOGIN_USER_KEY,r.getData());
        }
        return  r;
    }
    //获取登录的用户的信息
    @ApiOperation(value = "获取登录的用户" ,notes = "前台登录后获取登录的用户信息")
    @GetMapping(value = "/getCurrentUser")
    public ResponseUtil getCurrentUser(
            HttpSession session
    ){
        Object loginUser = session.getAttribute(ConstantUtil.LOGIN_USER_KEY);
        return  ResponseUtil.get(ResponseEnum.USER_API_GET_CURRENT_INFO,loginUser);

    }

    //前台注销
    @ApiOperation(value = "注销" ,notes = "前台登录用户注销")
    @GetMapping(value = "/logout")
    public ResponseUtil logout(
            HttpSession session
    ){
        //使session失效
        session.invalidate();
        return ResponseUtil.get(ResponseEnum.USER_API_LOGOUT_SUCCESS);
    }

    @ApiOperation(value = "注册账号" ,notes = "前台注册账号信息")
    @ApiImplicitParam(name = "userDto",value ="用户dto类" ,required = true,dataType = "UserDto")
    @PostMapping(value = "/register")
    public ResponseUtil register(
           @RequestBody UserDto userDto
    ){
        //phone +"-"+ code
        //包含手机号和验证码
        String phoneAndCode = stringRedisTemplate.opsForValue().get(userDto.getUuid());
        ResponseUtil r = null;
        if (phoneAndCode == null){
            //redis中的验证码不存在
            r = ResponseUtil.get(ResponseEnum.SMS_VERIFY_CODE_NOT_IN_REDIS);
        }else {
            //edis中的验证码存在
            //17614066482-1234
            String phoneCache = phoneAndCode.split("-")[0];
            String codeCache = phoneAndCode.split("-")[1];
            //判断
            if (phoneCache.equals(userDto.getPhone()) && codeCache.equals(userDto.getCode())){
                //缓存中手机号和验证码与注册的一致
                //这里调用service层完成注册功能
                r = userService.addApi(userDto);
                //注册成功，需要从缓存中删除对应的验证码
                stringRedisTemplate.delete(userDto.getUuid());
            }else {
                //缓存中手机号和验证码与注册的不一致
                r= ResponseUtil.get(ResponseEnum.SMS_VERIFY_CODE_NOT_MATCH);
            }
        }
      return  r;
    }
}
